﻿using System;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using ICeFramework.DataManagement.Interfaces;
using SharpGen.TemplateInterface;
using SharpGen.Common;
using Plugin;

namespace SharpGen
{
    public class TemplateServices : PluginHost<ICodeGenTemplate, ICodeGenHost>, ICodeGenHost
    {
        //private StringCollection _loadedAssemblies = new StringCollection();

        public string Database
        {
            get { return MainForm.Instance.SelectedDatabase; }
        }

        public SqlObjectCollection ObjectCollection
        {
            get { return MainForm.Instance.SelectedObjects; }
        }

        public string OutputPath
        {
            get { return MainForm.Instance.OutputPath; }
        }

        public string OutNamespace
        {
            get { return MainForm.Instance.OutputNamespace; }
        }


        #region public TemplateServices()
        /// <summary>
        /// Initializes a new instance of the <b>TemplateServices</b> class.
        /// </summary>
        public TemplateServices()
            : base(typeof(ICodeGenTemplate))
        {
        }
        #endregion

        //private AvailableTemplates colAvailableTemplates = new AvailableTemplates();
        //#region public AvailableTemplates AvailableTemplates
        ///// <summary>
        ///// Get/Sets the AvailableTemplates of the TemplateServices
        ///// </summary>
        ///// <value></value>
        //public AvailableTemplates AvailableTemplates
        //{
        //    get { return colAvailableTemplates; }
        //    set { colAvailableTemplates = value; }
        //}
        //#endregion

        //#region public void FindTemplates()
        ///// <summary>
        ///// 
        ///// </summary>
        //public void FindTemplates()
        //{
        //    FindTemplates(AppDomain.CurrentDomain.BaseDirectory);
        //}
        //#endregion

        //#region public void FindTemplates(string Path)
        ///// <summary>
        ///// Searches the passed Path for Plugins
        ///// </summary>
        ///// <param name="Path">Directory to search for Plugins in</param>
        //public void FindTemplates(string Path)
        //{
        //    colAvailableTemplates.Clear();

        //    if (Directory.Exists(Path))
        //    {
        //        foreach (string fileOn in Directory.GetFiles(Path))
        //        {
        //            FileInfo file = new FileInfo(fileOn);
        //            if (file.Extension.Equals(".dll"))
        //            {
        //                this.AddTemplate(fileOn, false);
        //            }
        //        }
        //    }
        //    else
        //    {
        //        Console.WriteLine(string.Format("Path not found: '{0}'", Path));
        //    }
        //}
        //#endregion

        //#region public void CloseTemplates()
        ///// <summary>
        ///// Unloads and Closes all AvailablePlugins
        ///// </summary>
        //public void CloseTemplates()
        //{
        //    foreach (AvailableTemplate pluginOn in colAvailableTemplates)
        //    {
        //        pluginOn.Instance.Dispose();
        //        pluginOn.Instance = null;
        //    }

        //    colAvailableTemplates.Clear();
        //}
        //#endregion

        //#region public void CloseTemplate(AvailableTemplate template)
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="template"></param>
        //public void CloseTemplate(AvailableTemplate template)
        //{
        //    template.Instance.Dispose();
        //    template.Instance = null;
        //}
        //#endregion

        //#region public bool AssemblyLoaded(string assemblyPath)
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="assemblyPath"></param>
        ///// <returns></returns>
        //public bool AssemblyLoaded(string assemblyPath)
        //{
        //    return _loadedAssemblies.Contains(assemblyPath);
        //}
        //#endregion

        //#region public void AddTemplate(string fileName)
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="fileName"></param>
        //public void AddTemplate(string fileName, bool clearTemplateList)
        //{
        //    if (clearTemplateList)
        //    {
        //        colAvailableTemplates.Clear();
        //    }
        //    Assembly templateAssembly = Assembly.LoadFrom(fileName);
        //    _loadedAssemblies.Add(fileName);

        //    foreach (Type pluginType in templateAssembly.GetTypes())
        //    {
        //        if (pluginType.IsPublic)
        //        {
        //            if (!pluginType.IsAbstract)
        //            {
        //                Type typeInterface = pluginType.GetInterface("CodeGen.TemplateInterface.ICodeGenTemplate", true);

        //                if (typeInterface != null)
        //                {
        //                    AvailableTemplate template = new AvailableTemplate();
        //                    template.AssemblyPath = fileName;

        //                    object obj = Activator.CreateInstance(templateAssembly.GetType(pluginType.ToString()));
        //                    template.Instance = (ICodeGenTemplate)obj;
        //                    template.Assembly = templateAssembly;
        //                    this.colAvailableTemplates.Add(template);
        //                }
        //                typeInterface = null;
        //            }
        //        }
        //    }
        //}
        //#endregion

        //#region public void InitializeTemplate(AvailableTemplate template)
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="template"></param>
        //public void InitializeTemplate(AvailableTemplate template)
        //{
        //    if (template != null)
        //    {
        //        try
        //        {
        //            template.Instance.Initialize(this);
        //        }
        //        catch (Exception ex)
        //        {
        //            MainForm.Instance.logTextBox.AppendText(string.Format("Error Executing Template {0}", ex.ToString()));
        //        }
        //    }
        //}
        //#endregion

        //public void OnSqlObject(AvailableTemplate template, SqlObject sqlObject, string outputFile, bool classGeneration)
        //{
        //    if (template == null)
        //    {
        //        return;
        //    }
        //    SharpCodeProvider provider = null;
        //    try
        //    {
        //        provider = new SharpCodeProvider(outputFile);
        //        template.Instance.OnSqlObject(provider, sqlObject, classGeneration);
        //    }
        //    catch (Exception ex)
        //    {
        //        MainForm.Instance.logTextBox.AppendText(string.Format("Error writing file {0}. {1}", outputFile, ex.ToString()));
        //    }
        //    finally
        //    {
        //        if (provider != null)
        //        {
        //            provider.Dispose();
        //            provider = null;
        //        }
        //    }
        //}

        //#region public void Feedback(ICodeGenTemplate template, string feedbackMessage)
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="template"></param>
        ///// <param name="feedbackMessage"></param>
        //public void Feedback(ICodeGenTemplate template, string feedbackMessage)
        //{
        //    MainForm.Instance.logTextBox.AppendText(feedbackMessage + "\r\n");
        //}
        //#endregion


    }
}
